#pragma once
#include <iostream>
#include "Protocol.hpp"
class Calculator
{
public:
    Response Execute(Request &req)
    {
        Response resp;
        switch (req.Oper())
        {
        case '+':
            resp.SetResult(req.X() + req.Y());
            break;
        case '-':
            resp.SetResult(req.X() - req.Y());
            break;
        case '*':
            resp.SetResult(req.X() * req.Y());
            break;
        case '/':
        {
            if (req.Y() == 0)
            {
                // 表示除0
                resp.SetCode(1);
            }
            break;
        }
        case '%':
        {
            if (req.Y() == 0)
            {
                // 表示模0
                resp.SetCode(2);
            }
            break;
        }
        default:
            //表示不认识这个请求
            resp.SetCode(3);
            break;
        }
        return resp;
    }

private:
}cal;

std::string HandlerRequest(std::string& package)
{
    std::string req_str;
    std::string result;
    //获得完整报文
    while(Decode(package,&req_str))
    {
        //反序列化
        Request req;
        if(!req.Deserialize(req_str))
            break;
        std::cout<<"########"<<std::endl;
        req.Print();
        std::cout<<"########"<<std::endl;
        //业务处理
        Response rsp = cal.Execute(req);
        std::string rsp_str;
        rsp.Serialize(rsp_str);
        Encode(rsp_str);
        result+=rsp_str;
    }
    return result;

}